export interface ICachesType {
  getCache: (key: string, isLocal: boolean) => any;
  setCache: (key: string, value: any, isLocal: boolean) => void;
  deleteCache: (key: string, isLocal: boolean) => void;
}

class ICaches implements ICachesType {
  getCache(key: string, isLocal = false) {
    let value = null;
    if (isLocal) {
      value = localStorage.getItem(key);
    } else {
      value = sessionStorage.getItem(key);
    }
    return value ? JSON.parse(value) : "";
  }
  setCache(key: string, value: any, isLocal = false) {
    const newValue = value ? JSON.stringify(value) : value;
    if (isLocal) {
      localStorage.setItem(key, newValue);
    } else {
      sessionStorage.setItem(key, newValue);
    }
  }
  deleteCache(key: string, isLocal = false) {
    if (isLocal) {
      localStorage.removeItem(key);
    } else {
      sessionStorage.removeItem(key);
    }
  }
}
export default new ICaches();
